<?php
include '../includes/cPlayer.php';
include '../includes/cDatabase.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Add Player");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

if(@$_GET['form'] == "yes")
{
	$error = 0;
	$cPlayer = new player($_POST['playerCode']);
	
	$error |= $cPlayer->addPlayerName($_POST['playerName']);
	$error |= $cPlayer->addPlayerTeam($_POST['playerTeam']);
	$error |= $cPlayer->addPlayerValue($_POST['playerValue']);
	$error |= $cPlayer->addPlayerPosition($_POST['playerPosition']);
	$error |= $cPlayer->addPlayerLastYearScore($_POST['playerLastYearScore']);
	
	if (!$error)
	{
		$cDatabase = new database();

		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);

		// Add a row to the table
		$cDatabase->insertPlayer($connect, $cPlayer->getPlayerCode(), $cPlayer->getPlayerName(),
			$cPlayer->getPlayerTeam(), $cPlayer->getPlayerValue(), $cPlayer->getPlayerPosition(), $cPlayer->getPlayerLastYearScore() );
	}
	else
	{
		echo "<BR>ERROR: Player has not been added";
	}
}

?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes" method="POST">
  <table width='400' border='0'>
    <tr>
      <td>Code</td>
      <td>Name</td>
      <td>Position</td>
      <td>Value</td>
      <td>Team</td>
	  <td>Score Last Year</td>
    </tr>
    <tr>
      <td><input type='text' name='playerCode' size='20' maxlength='4' value=<?php echo @$_POST['playerCode'] ?>></td>
      <td><input type='text' name='playerName' size='20' maxlength='30' value=<?php echo @$_POST['playerName'] ?>></td>
      <td><input type='text' name='playerPosition' size='20' maxlength='3' value=<?php echo @$_POST['playerPosition'] ?>></td>
      <td><input type='text' name='playerValue' size='20' maxlength='4' value=<?php echo @$_POST['playerValue'] ?>></td>
      <td><input type='text' name='playerTeam' size='20' maxlength='3' value=<?php echo @$_POST['playerTeam'] ?>></td>
	  <td><input type='text' name='playerLastYearScore' size='20' maxlength='3' value=<?php echo @$_POST['playerLastYearScore'] ?>></td>
    </tr></table>
	<table>       
    <td>
		<input name="addPlayerButton" type="submit" id="addPlayer" value="Add Player">        
	</td>
</table>
</form>

<?php
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>